/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.read;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.star.framework.excel.core.Column;
import cn.star.framework.excel.core.RowType;
import cn.star.framework.excel.core.RowWrapper;
import com.alibaba.excel.context.AnalysisContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;

/**
 * {@link ReadRowConverter}默认实现<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
public class SimpleReadRowConverter implements ReadRowConverter {

  @Override
  @SuppressWarnings({"unchecked", "rawtypes"})
  public RowWrapper convert(
      Map<Integer, Column> currentHeaderMap, Object data, AnalysisContext context) {
    // 当前行号
    Integer currentRowIndex = context.readRowHolder().getRowIndex();
    RowWrapper wrapper = new RowWrapper();
    Map m = (Map) data;
    List<Column> columns = new ArrayList<>(currentHeaderMap.values());
    Map record = this.buildRowWrapperData(m, columns);
    // 行数据默认为Map
    // 进行了数据类型转换，将对应字段的值替换了下标数字
    wrapper.setData(record);
    wrapper.setType(RowType.DATA);
    wrapper.setColumns(columns);
    wrapper.setIndex(currentRowIndex);

    return wrapper;
  }

  private Map<String, Object> buildRowWrapperData(Map<Integer, Object> map, List<Column> columns) {
    // 没有表头则直接抛出异常
    Assert.isFalse(
        CollectionUtil.isEmpty(columns),
        "No header, possibly due to an issue with the definition of columns in the Excel Sheet page");
    Map<String, Object> data = new HashMap<>(map.size());
    Set<Integer> keys = map.keySet();
    for (Integer key : keys) {
      Object value = map.get(key);
      Column column =
          columns.stream().filter(i -> key.equals(i.getOrder())).findFirst().orElse(null);
      // 多余的数据
      // `if (column == null) log.debug("col number: {}, excess data: {}", key, value);`
      if (column != null) {
        data.put(column.getKey(), value);
      }
    }

    return data;
  }
}
